package com.lyc.day1102.demo8;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author ：Xiaochen
 * @date ：Created in 2022/11/2 20:23
 * @description：
 * @modified By：
 * @version:
 */
public class Test {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建一个Callable类型的任务对象
        MyCallable myCallable = new MyCallable();
        //把任务返回的结果也封装到FtureTask对象中
        FutureTask task = new FutureTask(myCallable);
        //创建一个线程对象，把上面的任务交给该线程对象。
        Thread thread = new Thread(task);
        thread.start();
        System.out.println(task.get());
    }
}
//需要重写call方法，而且看到call方法有返回值。并且该方法有异常抛出
class MyCallable implements Callable{

    @Override
    public Integer call() throws Exception {
        int sum=0;
        for (int i = 1; i <= 100; i++) {
            sum+=i;
        }
        return sum;
    }
}